import { _decorator, Component, Node, BoxCollider2D, Contact2DType, Sprite, UITransform, Vec2, Vec3, CircleCollider2D, IPhysics2DContact } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { EventTypes } from '../../Init/Managers/EventTypes';
const { ccclass, property } = _decorator;

@ccclass('Grass2D')
export class Grass2D extends BasicComponet {
    collider: BoxCollider2D = null;
    setData(d: { parent: Node, p: Vec3, r: Vec3, s: Vec2 }) {
        this.node.parent = d.parent;
        this.node.setPosition(d.p);
        this.node.eulerAngles = d.r;
        let trans = this.node.getComponent(UITransform);
        // 注册单个碰撞体的回调函数
        this.collider = this.getComponent(BoxCollider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            this.collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
        //
        let sp = this.node.getComponent(Sprite);
        if (sp) {
            sp.enabled = GlobalConfig.isShow2dCollider;
        }
    }
    onDisable() {
        if (this.collider) {
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            this.collider.off(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }

    _zombieGroup = 1 << 6;
    _data = { cldId: '', zombieId: '', isHide: false };
    onBeginContact(selfCollider: BoxCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.group == this._zombieGroup) {
            this._data.cldId = this.node.uuid;
            this._data.zombieId = otherCollider.node.uuid;
            this._data.isHide = true;
            this.emit(EventTypes.RoleEvents.SetZombieHideState, this._data);
            // console.log('Hide')
        }
    }
    onEndContact(selfCollider: BoxCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.group == this._zombieGroup) {
            this._data.cldId = this.node.uuid;
            this._data.zombieId = otherCollider.node.uuid;
            this._data.isHide = false;
            this.emit(EventTypes.RoleEvents.SetZombieHideState, this._data);
            // console.log('un Hide')
        }
    }
}

